package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.FeeInfoMapper;
import cc.rengu.igas.bomp.common.entity.FeeInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName FeeInfoMapperImpl
 * @Description 费率模板表接口实现类
 * @Author 赵奎伍
 * @date 2020/3/30
 */
public class FeeInfoMapperImpl implements FeeInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public List<FeeInfo> selectFeeInfoByInstId(String instId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectFeeInfoByInstId");
        FeeInfo feeInfo = new FeeInfo();
        feeInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, feeInfo, FeeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取费率模板信息失败,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (FeeInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<FeeInfo> selectFeeInfoByKeys(String instId, String feeCd, String feeName) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectFeeInfoByKeys");
        FeeInfo feeInfo = new FeeInfo();
        feeInfo.setInstId(instId);
        if (null != feeCd && !feeCd.isEmpty()) {
            feeInfo.setFeeCd(feeCd);
        }
        if (null != feeName && !feeName.isEmpty()) {
            feeInfo.setFeeName(feeName);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, feeInfo, FeeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据费率代码、费率名称、费率描述查询费率信息失败,instId:<{}>,feeCd:<{}>,feeName:<{}>", instId, feeCd, feeName);
            return null;
        }
        return objectList.stream().map(item -> (FeeInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectFeeInfoPageFeeInfoByInstId(String instId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectFeeInfoByInstId");
        FeeInfo feeInfo = new FeeInfo();
        feeInfo.setInstId(instId);

        return dbsUtil.dbsFetchPageListData(sqlStr, feeInfo, FeeInfo.class, pageInfo);
    }

    @Override
    public PageResult selectFeeInfoPageInfoByKeys(String instId, String feeCd, String feeName, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectFeeInfoByKeys");
        FeeInfo feeInfo = new FeeInfo();
        feeInfo.setInstId(instId);
        if (!StringUtil.isEmptyOrNull(feeCd)) {
            feeInfo.setFeeCd(feeCd);
        }
        if (!StringUtil.isEmptyOrNull(feeName)) {
            feeInfo.setFeeName(feeName);
        }

        return dbsUtil.dbsFetchPageListData(sqlStr, feeInfo, FeeInfo.class, pageInfo);
    }
}
